package com.pagepub.expo.action.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.pagepub.expo.entity.AppFunction;
import com.pagepub.expo.entity.AppRole;
import com.pagepub.expo.manager.AppFunctionMng;
import com.pagepub.expo.manager.AppSeqMng;

public class AppFunctionAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	@Qualifier("appFunctionMng")
	private AppFunctionMng appFunctionMng;
	
	@Autowired
	@Qualifier("appSeqMng")
	private AppSeqMng appSeqMng;
	
	public List<AppFunction> rows = new ArrayList<AppFunction>();

	public int start = 0;

	private AppFunction appFunction;

	public int limit = 0;
	public int total;	
	public long newId = 0;
	private boolean success;

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
	public AppFunctionAction() {
		System.out.println("this is AppFunctionAction");
	}

	public String execute() throws Exception {
		System.out.println("this is execute");
		return null;
	}

	public String all() {
		HttpServletRequest request = ServletActionContext.getRequest();
		int start = Integer.valueOf(request.getParameter("start"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		String sortname = request.getParameter("sortname");
		String sortorder = request.getParameter("sortorder");
		HashMap<String, String> param = new HashMap<String, String>();
		if (sortname != null && sortname.length() > 0) {
			param.put("sortname", sortname);
		}
		if (sortorder != null && sortorder.length() > 0) {
			param.put("sortorder", sortorder);
		}
		rows = appFunctionMng.findByPage(start, limit, param);
		this.total = appFunctionMng.getCount(param);
		success = true;
		return SUCCESS;
	}


	public String delete() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("delIds");
		ids = ids.substring(0, ids.length()-1);
		StringTokenizer st = new StringTokenizer(ids, ",", false);
		List<String> StringList = new ArrayList<String>();
		while (st.hasMoreElements()) {
			StringList.add(st.nextToken());
		}
		for (int i = 0; i < StringList.size(); i++) {
			appFunctionMng.remove(new Long(StringList.get(i)));
		}
		return SUCCESS;
	}

	public String add() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String seqName = request.getParameter("seqName");
		newId = appSeqMng.getSequence(seqName);
		AppFunction appFunction = new AppFunction();
		appFunction.setFunctionId(new Long(newId));
		appFunction.setFunName("");
		appFunction.setFunKey("");
		appFunction.setFunctionOrder(new Long(0));
		appFunction.setFunctionLayer(new Long(0));
		appFunction.setParentId(new Long(0));
		appFunctionMng.add(appFunction);
		success = true;
		return SUCCESS;
	}

	public String modify() {
		appFunctionMng.update(appFunction);
		return SUCCESS;
	}
	

	public long getNewId() {
		return newId;
	}

	public void setNewId(long newId) {
		this.newId = newId;
	}

	public AppFunction getAppFunction() {
		return appFunction;
	}

	public void setAppFunction(AppFunction appFunction) {
		this.appFunction = appFunction;
	}

	public List<AppFunction> getRows() {
		return rows;
	}

	public void setRows(List<AppFunction> rows) {
		this.rows = rows;
	}	



	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	
	private String funListStr;
	public String getFunListStr() {
		return funListStr;
	}

	public void setFunListStr(String funListStr) {
		this.funListStr = funListStr;
	}
	//获取功能列表，用户前台checkbox使用
	public String getFunListForCheckbox() {
		List<AppFunction> temp = appFunctionMng.findAll();
		StringBuffer str = new StringBuffer();
		str.append('[');
		for (int i = 0; i < temp.size(); i++) {
			str.append("{boxLabel:'");
			str.append(temp.get(i).getFunName());
			str.append("' , name:'selectedFuns', inputValue:");
			str.append(temp.get(i).getFunctionId());
			str.append(", id:'fun"+temp.get(i).getFunctionId()+"'");
			str.append('}');
			if(i<temp.size()-1)
				str.append(',');
		}
		str.append(']');
		funListStr = str.toString();
		success = true;
		return SUCCESS;
	}
	
	
	private String tree;
	public String getFunListForTree() {
		tree = "[{text: 'To Do', cls: 'folder', children: [{text: 'Go jogging'," +
				"leaf: true, checked: false},{ text: 'Take a nap',leaf: true, checked: false" +
				" },{ text: 'Climb Everest',leaf: true,checked: false}]},{" +
				"text: 'Grocery List', cls: 'folder',children: [{ text: 'Bananas',leaf: true, checked: false" +
				" },{ text: 'Milk',leaf: true,checked: false},{text: 'Cereal',leaf: true,checked: false" +
				"}]}]";
		success = true;
		return SUCCESS;
	}

	public String getTree() {
		return tree;
	}

	public void setTree(String tree) {
		this.tree = tree;
	}
}
